import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';

import '../../../../../http/bean/http_data.dart';
import '../../../../../http/bean/worksdo/equipment_management_bean.dart';
import '../../../../../http/bean/worksdo/hardware_return_bean.dart';
import '../../../../../http/bean/worksdo/material_requisition_bean.dart';
import '../../../../../http/model/ajaxsales_model.dart';
import '../../../../../http/model/hardware_return_model.dart';
import '../../../../dialog/dailog.dart';
import '../../../helper/route_helper.dart';
import '../../../works/public/image/ui_image_upload.dart';
import '../public/items/ui_attachment.dart';
import '../public/acom/ui_ticketcenter_form.dart';
import '../public/items/ui_myworkorder.dart';
import '../public/items/ui_myworkorder2.dart';
import '../public/items/ui_myworkorder3.dart';
import '../public/review_submitted_myworkorder.dart';
import 'ui/review_submitted.dart';
import 'ui/ui_form.dart';
import 'ui/ui_info.dart';

///我的工单；工单领用（领用记录详情，五金退库）
class TreasuryListDetailsRoute extends StatefulWidget {
  @override
  _State createState() => _State();

  EquipmentInfo? equipmentInfo;
  HardwareReturnListBean? hardwareReturnListBean;

  TreasuryListDetailsRoute(this.equipmentInfo, this.hardwareReturnListBean);
}

class _State extends State<TreasuryListDetailsRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    if (presenter?.mDatas == null) {
      presenter?.mDatas = AppHttpResult<String, String>();
    }
    presenter?.mSubData?.equipmentInfo = widget.equipmentInfo;
    presenter?.mSubData?.hardwareReturnListBean = widget.hardwareReturnListBean;

    ///延迟获取数据
    presenter.GetRepairPersonListWithDelay(context, callback: () {});
  }

  @override
  Widget build(BuildContext context) {
    return ReviewDetailsUi.build(context, presenter, _body,
        title: AppStrings.of(context).Myworkorder +
            ":" +
            AppStrings.of(context).Receiptrecord); //我的工单:领用记录
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    UiImageUpLoad.clear();
    presenter?.mOldDatas?.Material = null;
    presenter?.mDatas?.Material = null;
    AjaxSalesModel.clear(); //清除物料编码数据缓存；
  }

  Widget _body(BuildContext context) {
    var mDatas = widget.hardwareReturnListBean;
    if (mDatas == null) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    children.add(UiInfo.build(
      context,
      mDatas,
      null,
      setState,
    )); //基本信息
    children.add(UiForm1.build(context, presenter?.mSubData?.datas,
        callbackWithVoid: () {
      //五金搜索条件回调
    })); //五金领用列表
    var b = false; //是否显示可退库按钮。
    presenter?.mSubData?.datas?.forEach((element) {
      var b2 = HardwareReturnListBean.isReturnEnable(element?.Status);
      if (b2) {
        b = true; //fixme 只要有一个条目，可以退库。都要显示退库按钮。
      }
    });
    var onlyBack = false;
    if (b) {
      //可以退库
      onlyBack = false;
    } else {
      onlyBack = true;
    }
    return Column(
      children: [
        Expanded(child: UiBase.listView(context, children)),
        Visibility(
            child: ReviewSubmittedStateful3(
              presenter?.mDatas, presenter?.mSubData,
              hasSave: presenter?.mDatas?.Material?.isCanoOeration(),
              //hasSave 保存
              hasSubmit: presenter?.mDatas?.Material?.isCanoOeration(),
              //hasSubmit 提交
              onlyBack: onlyBack,
              //onlyBack 是否只有一个返回键，其他什么操作都没有。
              callbackWithT: (subData) {
                //fixme 提交参数数据
                subData.purDemand = presenter?.mDatas?.Material;
              },
              callbackWithBoolResult: () {
                //fixme 提交数据格式验证
                return true;
              },
            ),
            visible: presenter?.mDatas != null)
      ],
    );
  }
}

class _Presenter extends ReviewPresenter {
  HardwareReturnSubData mSubData = HardwareReturnSubData();

  // EquipmentInfo? equipmentInfo;
  // HardwareReturnListBean? hardwareReturnListBean;
  // List<HardwareReturnBean>? datas;

  ///获取详情数据
  GetRepairPersonList(BuildContext context,
      {ResponseCallbackWithVoid? callback = null}) {
    isLoad = true;
    HardwareReturnModel.GetMaterialPickingListByMaterialPickingId(
        context, mSubData?.hardwareReturnListBean, true,
        onError: (code, error) {
      isLoad = false;
      setStateBody?.call(() {});
      AppDialog.showDialogWithError(context, code, error)?.then((value) {
        AppRouteHelper.pop(context); //网络异常，关闭当前页面
      });
    })?.then((value) {
      isLoad = false;
      if (value.isSuccessForCode()) {
        try {
          setStateBody?.call(() {
            mSubData?.datas = value.Data;
          });
          callback?.call();
        } catch (e) {}
      } else {
        //异常
        AppDialog.showDialogWithInfo(context, content: value.MsgDetail)
            ?.then((value) {
          AppRouteHelper.pop(context); //关闭当前页面
        });
      }
    });
  }

  ///获取审核详情数据[延迟获取，等待页面加载完成]
  GetRepairPersonListWithDelay(BuildContext context,
      {ResponseCallbackWithVoid? callback = null}) {
    Future.delayed(Duration(milliseconds: 200)).then((e) {
      GetRepairPersonList(context, callback: callback);
    });
  }
}
